package com.paipai.helper.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.paipai.helper.R;
import com.paipai.helper.util.CmdUtil;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.PermissionListener;
import com.yhao.floatwindow.Screen;
import com.yhao.floatwindow.ViewStateListener;

/**
 * 类描述：
 * <p>
 * 作者：TB.Liu on 19-3-12 13:17
 */
public class FloatingService extends Service implements View.OnClickListener {
    private FloatWindow.B mFloatWindow;
    private int mClickX;
    private int mClickY;
    private int mClickTempX;
    private int mClickTempY;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initView();
        return START_NOT_STICKY;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_helper_start:
//                mFloatWindow.setMoveType(MoveType.slide);
//                Toast.makeText(this, "开始", Toast.LENGTH_SHORT).show();
                CmdUtil.collectClick(this);
                break;
            case R.id.btn_save_position:
//                mFloatWindow.setMoveType(MoveType.slide);
                Toast.makeText(this, "保存", Toast.LENGTH_SHORT).show();
                mClickX = mClickTempX;
                mClickY = mClickTempY;
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 使用系统级别的WindowManager展示悬浮框，需要6.0以上的权限；
     */
    private void initView() {
        View view = LayoutInflater.from(this).inflate(R.layout.widget_chathead,null);
        view.findViewById(R.id.btn_helper_start).setOnClickListener(this);
        view.findViewById(R.id.btn_save_position).setOnClickListener(this);
        final TextView infoTv= (TextView)view.findViewById(R.id.btn_position_info);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    String toastInfo ="x:"+event.getX()+" y:"+event.getY();
                    Toast.makeText(FloatingService.this, toastInfo, Toast.LENGTH_SHORT).show();
                }

                return false;
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(FloatingService.this, "setOnClickListener", Toast.LENGTH_SHORT).show();
            }
        });
        final int viewWidth = view.getWidth();
        final int viewHeight = view.getHeight();

        mFloatWindow = FloatWindow
                .with(getApplicationContext())
                .setView(view)
//                .setWidth(100)                               //设置控件宽高
                .setHeight(Screen.height,0.2f)
                .setX(0)                                   //设置控件初始位置
                .setY(0)
                .setDesktopShow(true)                        //桌面显示
                .setMoveType(MoveType.active);
        mFloatWindow.setViewStateListener(new ViewStateListener() {
                    @Override
                    public void onPositionUpdate(int i, int i1) {
                        infoTv.setText(viewWidth+" : "+ viewHeight+" \nx:"+i+" y:"+i1);
                        mClickTempX = i;
                        mClickTempY = i1;
                    }

                    @Override
                    public void onShow() {

                    }

                    @Override
                    public void onHide() {

                    }

                    @Override
                    public void onDismiss() {

                    }

                    @Override
                    public void onMoveAnimStart() {

                    }

                    @Override
                    public void onMoveAnimEnd() {
                        infoTv.setText(viewWidth+" : "+ viewHeight+" \nx:"+FloatWindow.get().getX()+" y:"+FloatWindow.get().getY());
                    }

                    @Override
                    public void onBackToDesktop() {

                    }
                })
                .setPermissionListener(new PermissionListener() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFail() {

                    }
                })
                .build();

        FloatWindow.get().show();
    }
}
